{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-06T12:54:45.520459200Z",
     "start_time": "2023-11-06T12:54:45.334468Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(214, 10)"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "data_path = 'F:/机器学习数据集/glass.csv'\n",
    "glass = np.loadtxt(data_path,skiprows=1,delimiter=',')\n",
    "glass.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "(array([[1.52101e+00, 1.36400e+01, 4.49000e+00, 1.10000e+00, 7.17800e+01,\n         6.00000e-02, 8.75000e+00, 0.00000e+00, 0.00000e+00],\n        [1.51761e+00, 1.38900e+01, 3.60000e+00, 1.36000e+00, 7.27300e+01,\n         4.80000e-01, 7.83000e+00, 0.00000e+00, 0.00000e+00],\n        [1.51618e+00, 1.35300e+01, 3.55000e+00, 1.54000e+00, 7.29900e+01,\n         3.90000e-01, 7.78000e+00, 0.00000e+00, 0.00000e+00],\n        [1.51766e+00, 1.32100e+01, 3.69000e+00, 1.29000e+00, 7.26100e+01,\n         5.70000e-01, 8.22000e+00, 0.00000e+00, 0.00000e+00],\n        [1.51742e+00, 1.32700e+01, 3.62000e+00, 1.24000e+00, 7.30800e+01,\n         5.50000e-01, 8.07000e+00, 0.00000e+00, 0.00000e+00]]),\n array([1., 1., 1., 1., 1.]))"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X,y = glass[:,0:9],glass[:,9]\n",
    "X[:5],y[:5]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T12:56:05.020560800Z",
     "start_time": "2023-11-06T12:56:04.996561Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0,stratify=y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T12:58:32.094948900Z",
     "start_time": "2023-11-06T12:58:22.286397Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "array([1., 7., 2., 2., 2., 3., 3., 2., 1., 3.])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train[:10]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T12:59:08.092452200Z",
     "start_time": "2023-11-06T12:59:08.038451100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精度:100.000%\n",
      "测试集精度:70.370%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    "tree = DecisionTreeClassifier()\n",
    "tree.fit(X_train,y_train)\n",
    "print('训练集精度:{:.3f}%'.format(tree.score(X_train,y_train)*100))\n",
    "print('测试集精度:{:.3f}%'.format(tree.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:07:03.571087400Z",
     "start_time": "2023-11-06T13:07:03.516102900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 使用逻辑回归"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集:55.000%\n",
      "测试集:51.852%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "pipe_lr = Pipeline([('scaler',MinMaxScaler()),('pca',PCA()),('lr',LogisticRegression())])\n",
    "pipe_lr.fit(X_train,y_train)\n",
    "print('训练集:{:.3f}%'.format(pipe_lr.score(X_train,y_train)*100))\n",
    "print('测试集:{:.3f}%'.format(pipe_lr.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:15:27.938282300Z",
     "start_time": "2023-11-06T13:15:26.702609900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 使用随机森林"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集:100.000%\n",
      "测试集:79.630%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "rf = RandomForestClassifier()\n",
    "rf.fit(X_train,y_train)\n",
    "print('训练集:{:.3f}%'.format(rf.score(X_train,y_train)*100))\n",
    "print('测试集:{:.3f}%'.format(rf.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:09:08.397430Z",
     "start_time": "2023-11-06T13:09:08.234432600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集:79.630%\n"
     ]
    }
   ],
   "source": [
    "print('测试集:{:.3f}%'.format(rf.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:09:45.323866700Z",
     "start_time": "2023-11-06T13:09:45.300831400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最好深度: {'max_depth': 7}\n",
      "验证集最佳精度:73.715%\n",
      "测试集精度:81.481%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import GridSearchCV\n",
    "grid_rf = GridSearchCV(rf,n_jobs=-1,param_grid={'max_depth':[4,5,6,7,8]},cv=7)\n",
    "grid_rf.fit(X_train,y_train)\n",
    "print('最好深度:',grid_rf.best_params_)\n",
    "print('验证集最佳精度:{:.3f}%'.format(grid_rf.best_score_*100))\n",
    "print('测试集精度:{:.3f}%'.format(grid_rf.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:29:43.238906100Z",
     "start_time": "2023-11-06T13:29:41.769914500Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 使用神经网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精度:56.250\n",
      "测试集精度51.852\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\myAnaconda\\lib\\site-packages\\sklearn\\neural_network\\_multilayer_perceptron.py:582: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (200) reached and the optimization hasn't converged yet.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neural_network import MLPClassifier\n",
    "pipe_mlp = Pipeline([('scaler',MinMaxScaler()),('mlp',MLPClassifier())])\n",
    "pipe_mlp.fit(X_train,y_train)\n",
    "print('训练集精度:{:.3f}'.format(pipe_mlp.score(X_train,y_train)*100))\n",
    "print('测试集精度{:.3f}'.format(pipe_mlp.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:13:56.845047900Z",
     "start_time": "2023-11-06T13:13:56.651051900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 使用KNN"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精度:75.000%\n",
      "测试集精度:74.074%\n"
     ]
    }
   ],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "pipe_knn = Pipeline([(\"scaler\",MinMaxScaler()),('pca',PCA(5)),('knn',KNeighborsClassifier(n_neighbors=7))])\n",
    "pipe_knn.fit(X_train,y_train)\n",
    "print(\"训练集精度:{:.3f}%\".format(pipe_knn.score(X_train,y_train)*100))\n",
    "print(\"测试集精度:{:.3f}%\".format(pipe_knn.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:22:55.051354300Z",
     "start_time": "2023-11-06T13:22:55.021353500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精度:74.375%\n",
      "测试集精度:75.926%\n"
     ]
    }
   ],
   "source": [
    "pipe_knn = Pipeline([(\"scaler\",MinMaxScaler()),('knn',KNeighborsClassifier(n_neighbors=6))])\n",
    "pipe_knn.fit(X_train,y_train)\n",
    "print(\"训练集精度:{:.3f}%\".format(pipe_knn.score(X_train,y_train)*100))\n",
    "print(\"测试集精度:{:.3f}%\".format(pipe_knn.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:44:23.609310200Z",
     "start_time": "2023-11-06T13:44:23.584323300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集精度:78.750%\n",
      "测试集精度:70.370%\n"
     ]
    }
   ],
   "source": [
    "pipe_knn = Pipeline([('knn',KNeighborsClassifier(n_neighbors=5))])\n",
    "pipe_knn.fit(X_train,y_train)\n",
    "print(\"训练集精度:{:.3f}%\".format(pipe_knn.score(X_train,y_train)*100))\n",
    "print(\"测试集精度:{:.3f}%\".format(pipe_knn.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:21:31.434945300Z",
     "start_time": "2023-11-06T13:21:31.409937900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "邻居个数: {'knn__n_neighbors': 4}\n",
      "最佳验证集精度:64.992%\n",
      "测试集精度:74.074%\n"
     ]
    }
   ],
   "source": [
    "pipe_knn = Pipeline([(\"scaler\",MinMaxScaler()),('knn',KNeighborsClassifier())])\n",
    "grid_knn = GridSearchCV(pipe_knn,n_jobs=-1,cv=7,param_grid={'knn__n_neighbors':[4,5,6,7,8]})\n",
    "grid_knn.fit(X_train,y_train)\n",
    "print('邻居个数:',grid_knn.best_params_)\n",
    "print('最佳验证集精度:{:.3f}%'.format(grid_knn.best_score_*100))\n",
    "print('测试集精度:{:.3f}%'.format(grid_knn.score(X_test,y_test)*100))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-06T13:45:19.463860700Z",
     "start_time": "2023-11-06T13:45:19.379874Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
